import React, { useReducer } from 'react';

function ReducerDemo(){
    const [count,dispatch] = useReducer((state,action)=>{
        switch(action){
            case 'add':
                return state + 1
            break;
            case 'sub':
                return state - 1
            break;
                default:
                    return state
        }
    },10)
    return(
        <div>
            <h2>现在的分数是{count}</h2>
            <button onClick={()=>{dispatch('add')}}>increment</button>
            <button onClick={()=>{dispatch('sub')}}>Decrement</button>
        </div>
    )
}

export default ReducerDemo;
//reducer 原生
// function countReducer(state,action){
//     switch(action.type){
//         case 'add':
//             return state + 1
//         break;
//         case 'sub':
//             return state - 1
//         break;
//             default:
//                 return state
//     }
// }